import { pageTeacher } from '../../../../api/user'
import { getPrepareLessonPage } from '../../../../api/prepareLesson'
import { getTeachingResearchSearch } from '../../../../api/teachingResearch'
import { getWebCastPage } from '../../../../api/webCast'
import { pageAdvert } from '../../../../api/base'
import { formatTime } from '../../../../utils/util'
const app = getApp()

Component({
  properties: {
    scrollHeight: {
      type: Number,
      value: 0
    }
  },
  data: {
    swiperCategory: [],
    swiperData: [],
    swiper2Data: [],
    teacherParams: [],
    teachers: [],
    designs: [],
    activities: [],
    lives: [],
    token: '',
    cateId: '',
    teacherHeight: 275,
    isTeacherNoData: false,
    scrollY: true
  },
  options: {
    styleIsolation: 'apply-shared'
  },
  lifetimes: {
    attached() {
      this.init()
    }
  },
  attached() {
    this.init()
  },
  pageLifetimes: {
    show: function() {
      // 页面被展示
      this.setData({
        token: app.globalData.token || wx.getStorageSync('token')
      })
    }
  },
  methods: {
    scrollEvent() {
      // this.selectComponent('#moduleWrap').setData({ showCondition: false });
    },
    startScroll(){
      this.setData({ scrollY: true })
    },
    stopScroll(){
      this.setData({ scrollY: false })
    },
    init() {
      this.setData({
        token: app.globalData.token || wx.getStorageSync('token')
      })
      this.getPageAdvert01()
      this.getPageAdvert02()
      this.getDesignList()
      this.getActivityList()
      this.getLiveList()
    },
    setTeachCondition(e) {
      console.log(e.detail)
      if(!this.data.token) {
        this.setData({
          cateId: e.detail.subject
        })
      } else {
        this.getTeacherList(e.detail)
      }
    },
    getTeacherList(params) {
      pageTeacher({
        condition: {
          state: params.state,
          subject: params.subject
        },
        pager: {
          page: 1,
          pageSize: 5
        }
      }).then(res => {
        console.log('records',res)
        if (res.records.length) {
          this.setData({
            isTeacherNoData: false,
            teachers: res.records
          })
        } else {
          this.setData({isTeacherNoData: true})
        }
      })
    },
    bindLogin() {
      app.bindLogin().then(res => {
        this.setData({
          token: app.globalData.token || wx.getStorageSync('token')
        })
        this.getTeacherList(this.data.cateId)
      })
    },
    getPageAdvert01() {
      this.getPageAdvert('wxmHomeAdvert-1', 1)
    },
    getPageAdvert02() {
      this.getPageAdvert('wxmHomeAdvert-2', 2)
    },
    // 广告
    getPageAdvert(categoryCode, type){
      pageAdvert({
        condition: {
          categoryCode:categoryCode
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      }).then(res => {
        if(type === 1) {
          this.setData({
            swiperData: res.records
          })
        } else {
          this.setData({
            swiper2Data: res.records
          })
        }

      })
    },
    // 备课
    getDesignList() {
      getPrepareLessonPage({
        condition: {},
        pager: {
          page: 1,
          pageSize: 2
        }
      }).then(res => {
        if (res.records.length) {
          res.records.forEach(re => {
            re.createTime = formatTime(re.createTime, '{y}.{m}.{d}')
          })
        }
        this.setData({
          designs: res.records
        })
      })
    },
    // 教研
    getActivityList() {
      getTeachingResearchSearch({
        condition: {},
        pager: {
          page: 1,
          pageSize: 3
        }
      }).then(res => {
        if (res.records.length) {
          res.records.forEach(re => {
            re.createTime = formatTime(re.createTime)
            re.startTime = formatTime(re.startTime,'{y}.{m}.{d} {h}:{i}')
            re.endTime = formatTime(re.endTime)
          })
        }
        this.setData({
          activities: res.records
        })
      })
    },
    // 课程
    getLiveList() {
      getWebCastPage({
        condition: {},
        pager: {
          page: 1,
          pageSize: 2
        }
      }).then(res => {
        if (res.records.length) {
          res.records.forEach(re => {
            re.createTime = formatTime(re.createTime,'{y}.{m}.{d} {h}:{i}')
          })
        }
        this.setData({
          lives: res.records
        })
      })
    }
  }
})
